<template>
    <div style="width: 100%">
        <div id="deawer">
            <v-tableTitle :btnConfig="btnConfig"
                          @cancel="cancel"
                          @save="save"></v-tableTitle>
            <div class="detailBox">
                <v-detail ref="detail"></v-detail>
            </div>
            <div class="detailListBox">
                <v-detailList ref="detailList"></v-detailList>
            </div>
        </div>
    </div>

</template>

<script>
import baseMixin from "@/mixins/BaseMixin";
import skipMixin from "@/mixins/SkipMixin";
import tableTitle from "commons/TableTitle";
import detail from "../details/Detail"
import detailList from "../detailsList/DetailList"
import config from "../Config";

export default {
    name: "",
    mixins: [baseMixin, skipMixin, config],
    data() {
        return {
            newFlag: false,
        };
    },
    computed: {
        btnConfig() {
            return [
                {label: this.$t('common.save'), certigier: this.saveStatus||!this.btnPermNameList.includes('WM03070203'), invalid: "save"},
                {label: this.$t('common.cancel'),certigier: !this.btnPermNameList.includes('WM03070204'), invalid: "cancel"},
            ]
        }
    },
    created() {
    },
    methods: {
        //保存按钮
        async save() {
            let detailsData = this.$refs.detail.detailData;
            detailsData.allocatestrategykey = detailsData.allocatestrategykey.replace(/\s+/g, "");
            detailsData.descr = detailsData.descr.replace(/\s+/g, "")
            //非空判断
            if (detailsData.allocatestrategykey === '') {
                this.$message.error(this.$t('msg.msg13'))
                return
            }
            if (detailsData.descr === '') {
                this.$message.error(this.$t('msg.msg14'))
                return
            }

            let datailsListData = this.$refs.detailList.detailData;
            if (this.newFlag) {
                let data = {
                    ...detailsData,
                    allocatestrategydetailList: [
                        {
                            ...datailsListData
                        }
                    ]
                };
                try {
                    let res = await this.$api.post('/addallocatestrategy', data);
                    
                        this.$message.success(res.returnMgr);
                        this.cancel()
                    
                } catch (e) {
                    this.$message.error(e);
                }
            } else {
                if (this.$refs.detailList.detailNav != "1") {
                    let detailListData = this.$refs.detailList.detailData;
                    let tableData = this.$store.state.temporary;
                    if (this.$refs.detailList.detailsFlag) {
                        try {
                            let res = await this.$api.post('/queryallocatedetailmaxstepnumber', detailListData)
                            
                                try {
                                    detailListData.allocatestrategylinenumber = String((tableData.length + 1) * 10);
                                    let response = await this.$api.post('/addallocatestrategydetail', detailListData);
                                        this.$message.success(response.returnMgr);
                                        this.$refs.detailList.detailsFlag = false;
                                        this.$refs.detailList.detailNav = '1';
                                } catch (e) {
                                    this.$message.error(e);
                                }
                            
                        } catch (e) {
                            this.$message.error(e);
                        }
                    } else {
                        try {
                            let res = await this.$api.post('/updateallocatestrategydetail', detailListData);
                            
                                this.$message.success(res.returnMgr);
                                this.$refs.detailList.detailsFlag = false;
                                this.$refs.detailList.detailNav = '1';
                            
                        } catch (e) {
                            this.$message.error(e);
                        }
                    }

                } else {
                    try {
                        let res = await this.$api.post('/updateallocatestrategy', detailsData);
                        
                            this.$message.success(res.returnMgr);
                            this.cancel()
                        
                    } catch (e) {
                        this.$message.error(e);
                    }
                }

            }
        },
        //弹框取消
        cancel() {
            this.$router.push({name: "allocate"});
        },
    },
    mounted() {
        if (Object.keys(this.$store.state.skip).length !== 0) {
            let data = this.$store.state.skip;
            this.newFlag = data.newFlag;
        } else {
            this.newFlag = this.$store.state.newFlag;
        }
    },
    watch: {},
    components: {
        "v-tableTitle": tableTitle,
        "v-detail": detail,
        "v-detailList": detailList,
    }
}
</script>

<style scoped>
</style>
